﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ZoomExperiment
{
    public partial class SecondTest : PhoneApplicationPage
    {
        #region //----------------------------- Class Private Fields --------------------------------------//
        private double _initialScale;
        private double _currentScale;

        private Point _oldCenter;

        private Point _imagePosition;
        private Size _imageSize;

        private double _innerLimit;
        #endregion

        #region //----------------------------- Class Public Constructors ---------------------------------//
        public SecondTest()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(OnLoaded);
        }

        #endregion

        #region //----------------------------- Private Event Handlers ------------------------------------//
        private void OnLoaded(object sender, RoutedEventArgs args)
        {
            _imagePosition = new Point(0, 0);
            _imageSize = new Size(grid.ActualWidth, grid.ActualHeight);

            _innerLimit = Math.Min(grid.ActualWidth, grid.ActualHeight) * 0.2;
        }

        private void OnDragStarted(object sender, DragStartedGestureEventArgs args)
        {

        }

        private void OnDragDelta(object sender, DragDeltaGestureEventArgs args)
        {
            transform.TranslateX += args.HorizontalChange;
            transform.TranslateY += args.VerticalChange;
        }

        private void OnDragCompleted(object sender, DragCompletedGestureEventArgs args)
        {

        }

        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs args)
        {
            _initialScale = transform.ScaleX;

            _oldCenter = args.GetPosition(image, 0).CenterWith(args.GetPosition(image, 1));
            _oldCenter.X *= transform.ScaleX;
            _oldCenter.Y *= transform.ScaleY;
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs args)
        {
            _currentScale = _initialScale * args.DistanceRatio;

            // Tổng độ dịch chuyển trong lần pinch này
            Point totalTransitionDelta = new Point(0, 0);

            // Độ dịch chuyển của tâm khi không tính dịch chuyển theo tâm ngón tay
            Point oldCenterTransitionDelta = CalculateOldCenterTransitionDelta();
            totalTransitionDelta.X -= oldCenterTransitionDelta.X;
            totalTransitionDelta.Y -= oldCenterTransitionDelta.Y;

            transform.ScaleX = transform.ScaleY = _initialScale * args.DistanceRatio;
            transform.TranslateX += totalTransitionDelta.X;
            transform.TranslateY += totalTransitionDelta.Y;
        }

        private void OnPinchCompleted(object sender, PinchGestureEventArgs args)
        {

        }
        #endregion

        #region //----------------------------- Class Private Methods -------------------------------------//
        private Point CalculateOldCenterTransitionDelta()
        {
            // Tọa độ tâm cũ nếu không áp dụng scale
            Point originalOldCenter = new Point(
                _oldCenter.X / transform.ScaleX,
                _oldCenter.Y / transform.ScaleY);

            // Tọa độ của tâm cũ khi áp dụng scale mới
            Point scaledOriginalOldCenter = new Point(
                originalOldCenter.X * _currentScale,
                originalOldCenter.Y * _currentScale);

            // Độ dịch của tâm cũ sau khi áp dụng scale mới
            Point centerTransitionDelta = new Point(
                scaledOriginalOldCenter.X - _oldCenter.X,
                scaledOriginalOldCenter.Y - _oldCenter.Y);

            // Gán tâm cũ bằng tâm sau khi đã dịch
            _oldCenter = scaledOriginalOldCenter;

            return centerTransitionDelta;
        }
        #endregion
    }
}